package com.solrj.test;

import java.net.MalformedURLException;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

public class SolrjQuery { 
    private String url; 
    private Integer soTimeOut; 
    private Integer connectionTimeOut; 
    private Integer maxConnectionsPerHost; 
    private Integer maxTotalConnections; 
    private Integer maxRetries; 
    private CommonsHttpSolrServer solrServer = null; 
    private final static String ASC = "asc"; 
 
    public void init() throws MalformedURLException { 
        solrServer = new CommonsHttpSolrServer(url); 
        solrServer.setSoTimeout(soTimeOut); 
        solrServer.setConnectionTimeout(connectionTimeOut); 
        solrServer.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost); 
        solrServer.setMaxTotalConnections(maxTotalConnections); 
        solrServer.setFollowRedirects(false); 
        solrServer.setAllowCompression(true); 
        solrServer.setMaxRetries(maxRetries); 
    } 
 
    public SolrDocumentList query(Map<String, String> propertyMap, 
            Map<String, String> compositorMap, Long startIndex, Long pageSize) 
            throws Exception { 
        SolrQuery query = new SolrQuery(); 
        // 设置搜索字段 
        if (null == propertyMap) { 
            throw new Exception("搜索字段不可为空!"); 
        } else { 
            for (Object o : propertyMap.keySet()) { 
                StringBuffer sb = new StringBuffer(); 
                sb.append(o.toString()).append(":"); 
                sb.append(propertyMap.get(o)); 
                String queryString = addBlank2expression_r(sb.toString()); 
                query.setQuery(queryString); 
            } 
        } 
        // 设置排序条件 
        if (null != compositorMap) { 
            for (Object co : compositorMap.keySet()) { 
                if (ASC == compositorMap.get(co) 
                        || ASC.equals(compositorMap.get(co))) { 
                    query.addSortField(co.toString(), SolrQuery.ORDER.asc); 
                } else { 
                    query.addSortField(co.toString(), SolrQuery.ORDER.desc); 
                } 
            } 
        } 
         
        if (null != startIndex) { 
            query.setStart(Integer.parseInt(String.valueOf(startIndex))); 
        } 
        if (null != pageSize && 0L != pageSize.longValue()) { 
            query.setRows(Integer.parseInt(String.valueOf(pageSize))); 
        } 
        try { 
            QueryResponse qrsp = solrServer.query(query); 
            SolrDocumentList docs = qrsp.getResults(); 
            return docs; 
        } catch (Exception e) { 
            throw new Exception(e); 
        } 
    } 
 
    private String addBlank2expression_r(String oldExpression) { 
        String lastExpression; 
        lastExpression = oldExpression.replace("AND", " AND ").replace("NOT", 
                " NOT ").replace("OR", " OR "); 
        return lastExpression; 
    } 
 
} 